package com.tq.tuqupro;

import java.util.List;
import java.util.Map;

import com.tq.application.TBaseApplication;
import com.tq.http.TBaseHttpClient;
import com.tq.util.EtIbUtil;
import com.tq.util.InternetUtil;
import com.tq.util.PreferenceUtil;
import com.tq.util.ShowUtil;
import com.tq.util.StringUtil;
import com.tuqu.asynctask.AsyncTaskListener;

import android.R;
import android.R.integer;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.ToggleButton;

public abstract class TBaseActivity extends Activity implements AsyncTaskListener {

	private ShowUtil showDialog;
	private InternetUtil internetUtil;
	private TBaseHttpClient httpClient;
	private StringUtil stringUtil;
	private PreferenceUtil preferenceUtil;
	private EtIbUtil etIbUtil;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(setLayoutId());
		showDialog = new ShowUtil(this);
		internetUtil = TBaseApplication.getInternetUtil();
		httpClient = TBaseApplication.getHttpClient();
		stringUtil = TBaseApplication.getStringUtil();
		preferenceUtil=TBaseApplication.getPreferenceUtil();
		etIbUtil=TBaseApplication.getEtIbUtil();
	}

	
	
	public abstract int setLayoutId();

	// 初始化控件
	public Button initBtn(int id) {

		Button btn = (Button) this.findViewById(id);
		return btn;
	}

	public TextView initTv(int id) {
		TextView tv = (TextView) this.findViewById(id);
		return tv;
	}

	public EditText initEt(int id)
	{
		EditText et = (EditText) this.findViewById(id);
		return et;
	}
	public ImageButton initIb(int id) {
		ImageButton ib = (ImageButton) this.findViewById(id);
		return ib;
	}

	public ImageView initIv(int id) {
		ImageView iv = (ImageView) this.findViewById(id);
		return iv;
	}

	public RadioButton initRb(int id) {
		RadioButton rb = (RadioButton) this.findViewById(id);
		return rb;
	}

	public RadioGroup initRp(int id) {
		RadioGroup rp = (RadioGroup) this.findViewById(id);
		return rp;
	}

	public ProgressBar initPg(int id) {
		ProgressBar pg = (ProgressBar) this.findViewById(id);
		return pg;
	}

	public SeekBar initSb(int id) {
		SeekBar sb = (SeekBar) this.findViewById(id);
		return sb;
	}
	
	public Spinner initSp(int id) {
		Spinner sp = (Spinner) this.findViewById(id);
		return sp;
	}

	public ListView initLv(int id) {
		ListView lv = (ListView) this.findViewById(id);
		return lv;
	}

	public GridView initGv(int id) {
		GridView gv = (GridView) this.findViewById(id);
		return gv;
	}

	public Gallery initGy(int id) {
		Gallery gy = (Gallery) this.findViewById(id);
		return gy;
	}

	public ToggleButton initTb(int id) {
		ToggleButton tb = (ToggleButton) this.findViewById(id);
		return tb;
	}

	// 初始化某个Dialog的控件,dlg为相应的Dialog
	public Button initBtn(Dialog dlg, int id) {

		Button btn = (Button) dlg.findViewById(id);
		return btn;
	}

	public TextView initTv(Dialog dlg, int id) {
		TextView tv = (TextView) dlg.findViewById(id);
		return tv;
	}
	public EditText initEt(Dialog dlg,int id)
	{
		EditText et = (EditText) dlg.findViewById(id);
		return et;
	}
	public ImageButton initIb(Dialog dlg, int id) {
		ImageButton ib = (ImageButton) dlg.findViewById(id);
		return ib;
	}

	public ImageView initIv(Dialog dlg, int id) {
		ImageView iv = (ImageView) dlg.findViewById(id);
		return iv;
	}

	public RadioButton initRb(Dialog dlg, int id) {
		RadioButton rb = (RadioButton) dlg.findViewById(id);
		return rb;
	}

	public RadioGroup initRp(Dialog dlg, int id) {
		RadioGroup rp = (RadioGroup) dlg.findViewById(id);
		return rp;
	}

	public ProgressBar initPg(Dialog dlg, int id) {
		ProgressBar pg = (ProgressBar) dlg.findViewById(id);
		return pg;
	}

	public SeekBar initSb(Dialog dlg, int id) {
		SeekBar sb = (SeekBar) dlg.findViewById(id);
		return sb;
	}
	
	public Spinner initSp(Dialog dlg,int id) {
		Spinner sp = (Spinner) dlg.findViewById(id);
		return sp;
	}

	public ListView initLv(Dialog dlg, int id) {
		ListView lv = (ListView) dlg.findViewById(id);
		return lv;
	}

	public GridView initGv(Dialog dlg, int id) {
		GridView gv = (GridView) dlg.findViewById(id);
		return gv;
	}

	public Gallery initGy(Dialog dlg, int id) {
		Gallery gy = (Gallery) dlg.findViewById(id);
		return gy;
	}

	public ToggleButton initTb(Dialog dlg, int id) {
		ToggleButton tb = (ToggleButton) dlg.findViewById(id);
		return tb;
	}

	// 初始化某个View的控件,v代表初始化某个View
	public Button initBtn(View v, int id) {

		Button btn = (Button) v.findViewById(id);
		return btn;
	}

	public TextView initTv(View v, int id) {
		TextView tv = (TextView) v.findViewById(id);
		return tv;
	}
	
	public EditText initEt(View v, int id) {
		EditText et = (EditText) v.findViewById(id);
		return et;
	}

	public ImageButton initIb(View v, int id) {
		ImageButton ib = (ImageButton) this.findViewById(id);
		return ib;
	}

	public ImageView initIv(View v, int id) {
		ImageView iv = (ImageView) v.findViewById(id);
		return iv;
	}

	public RadioButton initRb(View v, int id) {
		RadioButton rb = (RadioButton) v.findViewById(id);
		return rb;
	}

	public RadioGroup initRp(View v, int id) {
		RadioGroup rp = (RadioGroup) v.findViewById(id);
		return rp;
	}

	public ProgressBar initPg(View v, int id) {
		ProgressBar pg = (ProgressBar) v.findViewById(id);
		return pg;
	}

	public SeekBar initSb(View v, int id) {
		SeekBar sb = (SeekBar) v.findViewById(id);
		return sb;
	}
	
	public Spinner initSp(View v,int id) {
		Spinner sp = (Spinner) v.findViewById(id);
		return sp;
	}

	public ListView initLv(View v, int id) {
		ListView lv = (ListView) v.findViewById(id);
		return lv;
	}

	public GridView initGv(View v, int id) {
		GridView gv = (GridView) v.findViewById(id);
		return gv;
	}

	public Gallery initGy(View v, int id) {
		Gallery gy = (Gallery) v.findViewById(id);
		return gy;
	}

	public ToggleButton initTb(View v, int id) {
		ToggleButton tb = (ToggleButton) v.findViewById(id);
		return tb;
	}

	// 异步操作
	@Override
	public void onBeforeTask(int taskId) {
		// TODO Auto-generated method stub

	}

	@Override
	public Object onTask(int taskId, Object... params) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onAfterTask(int taskId, Object result) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTaskError(int taskId, Exception exception) {
		// TODO Auto-generated method stub

	}

	// 创建普通Dialog
	public Dialog createDlg(int style, int dlgview, boolean isTouchCancel,
			int gravity) {
		return showDialog.createDlg(style, dlgview, isTouchCancel, gravity);
	}

	// 异步操作显示Dialog
	public void showPgDlg(String title, String msg, int pgicon) {

		showDialog.showPgDlg(title, msg, pgicon);

	}

	// 异步操作关闭Dialog
	public void closePgDlg() {
		showDialog.closePgDlg();

	}

	// 显示Toast,0为TimeShort,1为TimeLong
	public void showToast(int flag, String msg) {
		showDialog.showToast(flag, msg);
	}

	// 判断网络是否可用
	public boolean isInternet() {
		return internetUtil.isInternet();
	}

	// 判断网络是否为wifi
	public boolean isWifi() {
		return internetUtil.isWifi();
	}

	// 判断网络是否为3g
	public boolean is3G() {
		return internetUtil.is3G();
	}

	public TBaseHttpClient httpClient() {
		return httpClient;
	}

	// 校验邮箱
	public boolean isEmail(String str) {
		int i = stringUtil.checkEmail(str);
		if (i == 0) {

			return false;
		} else if (i == -1) {

			return false;
		} else {
			return true;
		}
	}

	// 校验手机号
	public boolean isPhone(String str) {
		int i = stringUtil.checkPhone(str);
		if (i == 0) {

			return false;
		} else if (i == -1) {

			return false;
		} else {
			return true;
		}
	}

	// 校验字符串长度
	public boolean isLength(String str, int min, int max) {
		int i = stringUtil.checkLength(str, min, max);
		if (i == 0) {
			return false;
		} else if (i == -1) {
			return false;
		} else {
			return true;
		}
	}
	
	//解析Struts返回的结果 "error"转error
	public String paresStru_str(String str)
	{
		return stringUtil.parseStr(str);
	}
	
	
	//存储String类型的preference
	public boolean saveStrPref(String filename,String key,String value)
	{
		preferenceUtil.getPreference(filename);
		return preferenceUtil.saveStrPreference(key, value);
	}
	
	//得到String类型的preference
	public String getStrPref(String filename,String key,String defaultStr)
	{
		preferenceUtil.getPreference(filename);
		return preferenceUtil.getStrPreference(key, defaultStr);
	}
	
	
	//存储Int类型的preference
	public boolean saveIntPref(String filename,String key,int value)
	{
		preferenceUtil.getPreference(filename);
		return preferenceUtil.saveIntPreference(key, value);
	}
	
	//得到Int类型的preference
	public int getIntPref(String filename,String key)
	{
		preferenceUtil.getPreference(filename);
		return preferenceUtil.getIntPreference(key);
	}
	//存储Float类型的preference
	public boolean saveFloatPref(String filename,String key,Float value)
	{
		preferenceUtil.getPreference(filename);
		return preferenceUtil.saveFloatPreference(key, value);
	}
	//得到Float类型的preference
	public float getFloatPref(String filename,String key)
	{
		preferenceUtil.getPreference(filename);
		return preferenceUtil.getFloatPreference(key);
	}
	//存储Long类型的preference
	public boolean saveLongPref(String filename,String key,Long value)
	{
		preferenceUtil.getPreference(filename);
		return preferenceUtil.saveLongPreference(key, value);
	}
	//得到Long类型的preference
	public long getLongPref(String filename,String key)
	{
		preferenceUtil.getPreference(filename);
		return preferenceUtil.getLongPreference(key);
	}
	//存储boolean类型的preference
	public boolean saveBoolPref(String filename,String key,boolean value)
	{
		preferenceUtil.getPreference(filename);
		return preferenceUtil.saveBoolPreference(key, value);
	}
	//得到boolean类型的preference
	public boolean getBoolPref(String filename,String key)
	{
		preferenceUtil.getPreference(filename);
		return preferenceUtil.getBoolPreference(key);
	}
	//存储全部的preference
	public boolean saveAllPref(String filename,List<?> list,String...keyName)
	{
		preferenceUtil.getPreference(filename);
		
		return preferenceUtil.saveAllPreference(list,keyName);
	}
	//得到全部的preference
	public Map<String, ?> getAllPref(String filename)
	{
		preferenceUtil.getPreference(filename);
		return preferenceUtil.getAllPreference();
	}
	//清除某个Key的Preference
	public boolean clearPref(String filename,String key)
	{
		preferenceUtil.getPreference(filename);
		return preferenceUtil.removeKey(key);
		
	}
	//清除全部的Preference
	public boolean clearAllPref(String filename)
	{
		preferenceUtil.getPreference(filename);
		return preferenceUtil.removeAllKey();
		
	}
	
	public void ibChangeText(EditText et,ImageButton ib)
	{
		etIbUtil.changeIb(et, ib);
	}
	
	
	
}
